import router from "@/router";
import storage from '@/utils/storage'
import store from '@/store'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
router.beforeEach((to,from,next)=>{
    ////出现进度条
    NProgress.start()
    ////隐藏转圈圈
    NProgress.configure({showSpinner: false});
    //! to 目标路由
    //! from 源路由对象
    //! next是这个函数，调用之后，可以理解为放行
    let token = storage.get('token')
    if(token){
        ////已经登录 =>放行
        // next()
        ////判断有没有加载菜单
        if(!store.state.permission.userMenu.length){
            ////没有菜单数据就发起请求
            store.dispatch('permission/FETCH_MENU_LIST')
            .then(()=>{
                next({ path: to.path , replace:true , query:to.query})
            })        
        }else{
            next()
        }
    }else{
        ////没有登录
        ////=>访问的是登录页
        ////=>访问的不是登录页
        if(to.path === '/login'){
            next()
        }else{
            next({path:'/login'})
        }
       
    }

    
    
})

router.afterEach((to,from)=>{
    ////关闭进度条
    NProgress.done()
    // console.log(to);
    ////matched属性是匹配成功的路由对象，且第0项为空的不可用
    let match = to.matched.filter((item,index)=>index>0)
    console.log(match);
    store.commit('permission/GET_BREAD',match)

    
})



export default router